package framework.ia;

import org.newdawn.slick.GameContainer;

import framework.BasicFighter;
import framework.Controls;
import framework.Util;
import framework.attack.BasicAttack;

public class ReactorBrain extends FollowAndAttackBrain {

	/** Where the fighter is supposed to go, -1 if he doesn't have to go anywhere */
	int moveGoalX;

	int currentMove;

	int currentParry;

	int parryGoal;

	public ReactorBrain(BasicFighter fighter, BasicFighter fighter2) {
		super(fighter, fighter2);
		moveGoalX = -1;
		parryGoal = -1;
		currentMove = 0;
		currentParry = 0;
	}

	@Override
	public void update(GameContainer gc, int delta) {
		currentMove += Math.abs(getHostFighter().getXspeed());
		currentParry = getHostFighter().isParrying() ? currentParry + delta : 0;
		System.out.println("currentp " + currentParry);
		System.out.println("parryGoal " + parryGoal);
		if (currentMove >= moveGoalX || getHostFighter().getXspeed() == 0) {
			moveGoalX = -1;
			currentMove = 0;
		}

		if (currentParry >= parryGoal) {
			parryGoal = -1;
			currentParry = 0;
			getHostFighter().setParrying(false);
		}

		if (moveGoalX == -1 && parryGoal == -1) {
			if(fighter2.getXspeed() != 0 || fighter2.isJumping() || fighter2.isAttacking()) {
				if (fighter2.getXspeed() != 0) {
					reactOnMove(fighter2.isForward());
				}
				if (fighter2.isJumping()) {
					reactOnJump();
				}
				if (fighter2.isAttacking() && fighter2.getCurrentAttack().getHitBox() != null) {
					reactOnAttack();
				}
			} else {
				reactDefault();
			}
		}
	}

	public void reactOnJump() {

	}

	public void reactOnMove(boolean forward) {
		int prob = Util.randomize(0, 100);
		if(prob <= 15) {
			getHostFighter().jump(10);
		} else if(prob <= 60) {
			getHostFighter().move(Util.randomize(1, 2) == 1);
			moveGoalX = Util.randomize(50, 150);
		} else {
			pickAttack();
			BasicAttack<?> att = getHostFighter().getAttacks().getAttack(Controls.B_P1);
			att.setAttackCollisionManager(collisions);
			getHostFighter().attack(att);
		}
	}

	public void reactOnAttack() {
		if (fighter2.getCurrentAttack().getHitBox() == null) {
			return;
		}
		int x = (int) fighter2.getCurrentAttack().getHitBox().getCenterX();
		int x1 = (int) getHostFighter().getHitBox().getCenterX();
		int distance = Math.abs(x - x1);
		BasicAttack<?> att;
		if (distance < getHostFighter().getCurrentAttack().getWidth() * 8) {
			if (Util.randomize(1, 2) == 1) {
				getHostFighter().setParrying(true);
				if (getHostFighter().isParrying()) {
					parryGoal = 400;
				}
			} else { // counter
				att = getHostFighter().getAttacks().getAttack(Controls.A_P1);
				att.setAttackCollisionManager(collisions);
				getHostFighter().attack(att);
			}
		}
	}

	public void reactDefault() {
		reactOnMove(true);
	}

	public void pickAttack() {
		int x = (int) fighter2.getHitBox().getCenterX();
		int x1 = (int) getHostFighter().getHitBox().getCenterX();
		int distance = Math.abs(x - x1);
		BasicAttack<?> att;
		if (distance < getHostFighter().getHitBox().getWidth() * 8) {
			att = getHostFighter().getAttacks().getAttack(Controls.A_P1);
		} else if (distance < getHostFighter().getHitBox().getWidth() * 12) {
			att = getHostFighter().getAttacks().getAttack(Controls.X_P1);
		} else {
			att = getHostFighter().getAttacks().getAttack(Controls.B_P1);
		}
		att.setAttackCollisionManager(collisions);
		getHostFighter().attack(att);
	}
}
